package com.hengpeng.itfin.domain.partner.chinapnr.util;

import java.math.BigInteger;

public class PrivateKey {
	protected byte[] Modulus;
	protected byte[][] Prime;
	protected byte[][] PrimeExponent;
	protected byte[] Coefficient;
	protected final String DESKEY = "SCUBEPGW";
	
	public PrivateKey(String keygen , boolean isPriKey){
		buildKey(keygen, isPriKey);
	}
	
	public PrivateKey(String keygen){
		buildKey(keygen, true);
	}
	
	private void buildKey(String keygen , boolean isPriKey){
	    this.Modulus = new byte[''];
	    this.Prime = new byte[2][64];
	    this.PrimeExponent = new byte[2][64];
	    this.Coefficient = new byte[64];
	    
	    String str1;
	    
        if (isPriKey)
        	str1 = keygen.substring(80, keygen.length());
        else
        	str1 = keygen.substring(48, keygen.length());

    	byte[] arrayOfByte1 = new byte[8];
        DesKey localDesKey = new DesKey(DESKEY.getBytes(), false);
        Des localDes = new Des(localDesKey);
        BigInteger localBigInteger = new BigInteger(str1, 16);
        byte[] arrayOfByte2 = localBigInteger.toByteArray();
        if (arrayOfByte2[0] == 0) {
        	if (isPriKey){
        		for (int j = 0; j < 712; j++) {
        			arrayOfByte2[j] = arrayOfByte2[(j + 1)];
        		}
        	}

        	for (int j = 0; j < 264; j++) {
        		arrayOfByte2[j] = arrayOfByte2[(j + 1)];
        	}
        }
        System.arraycopy(arrayOfByte2, 0, this.Modulus, 0, 128);
        if (isPriKey)
        {
          memset(arrayOfByte1, (byte)0, arrayOfByte1.length);
          localDes.cbc_encrypt(arrayOfByte2, 384, 64, this.Prime[0], 0, arrayOfByte1, false);
          memset(arrayOfByte1, (byte)0, arrayOfByte1.length);
          localDes.cbc_encrypt(arrayOfByte2, 448, 64, this.Prime[1], 0, arrayOfByte1, false);
          memset(arrayOfByte1, (byte)0, arrayOfByte1.length);
          localDes.cbc_encrypt(arrayOfByte2, 512, 64, this.PrimeExponent[0], 0, arrayOfByte1, false);
          memset(arrayOfByte1, (byte)0, arrayOfByte1.length);
          localDes.cbc_encrypt(arrayOfByte2, 576, 64, this.PrimeExponent[1], 0, arrayOfByte1, false);
          memset(arrayOfByte1, (byte)0, arrayOfByte1.length);
          localDes.cbc_encrypt(arrayOfByte2, 640, 64, this.Coefficient, 0, arrayOfByte1, false);
        }        
	}
	private void memset(byte[] paramArrayOfByte, byte paramByte, int paramInt){
	    for (int i = 0; i < paramInt; i++)
	      paramArrayOfByte[i] = paramByte;
	}
}
